package LeetCode;

public class LC_396_RotateFunction {

    public static void main(String[] args) {

    }

    public class Solution {
        public int maxRotateFunction(int[] A) {
            int n = A.length;
            int f0 = 0;
            int sumOfA = 0;
            for (int i = 0; i < n; i++) {
                f0 += A[i] * i;
                sumOfA += A[i];
            }
            int max = f0;
            int fi = f0;
            for (int i = 1; i < n; i++) {
                fi += sumOfA;
                fi -= n * A[n - i];
                max = Math.max(max, fi);
            }
            return max;
        }

    }
}
